package lib

import (
	"algorithms/ds"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         19,
		Discription: "删除链表倒数n个节点",
		Level:       2,
		Labels: map[string]int{
			"双指针滑动": 1,
			"链表":    1,
			"快慢指针": 1,
			"虚拟头指针": 1,
		},
	})
}

func RemoveNthFromEnd(head *ds.ListNode, n int) *ds.ListNode {
	temp1 := head
	for i := 0; i < n; i++ {
		temp1 = temp1.Next
	}

	//考虑到删除第一个节点的情况
	preHead := &ds.ListNode{
		Next: head,
	}
	temp2 := preHead
	for temp1 != nil {
		temp1 = temp1.Next
		temp2 = temp2.Next
	}
	temp2.Next = temp2.Next.Next

	return preHead.Next
}
